package cn.bonoon.kernel.web.handlers.loader;

import java.lang.reflect.Field;
import java.util.Collection;

import org.springframework.context.ApplicationContext;

import cn.bonoon.handler.impl.StandardAutoManager;
import cn.bonoon.kernel.bytecode.BytecodeFactory;
import cn.bonoon.kernel.bytecode.EntityReader;
import cn.bonoon.kernel.descriptors.DescriptorFactory;
import cn.bonoon.kernel.descriptors.ObjectDescriptor;
import cn.bonoon.kernel.descriptors.ObjectPropertyAccessor;
import cn.bonoon.kernel.events.ReadEvent;
import cn.bonoon.kernel.support.models.Item;
import cn.bonoon.kernel.support.models.Page;
import cn.bonoon.kernel.support.searcher.Finder;
import cn.bonoon.kernel.support.searcher.FinderHandler;
import cn.bonoon.kernel.support.searcher.ItemSelectedFilter;
import cn.bonoon.kernel.support.searcher.StandardFinder;
import cn.bonoon.kernel.web.annotations.AutoDataLoader;
import cn.bonoon.kernel.web.annotations.AutoDataResultClass;

/**
 * <pre>
 * 不管是否使用分页，combogrid都应该单独使用自己的处理器
 * 
 * 传过去的查询条件应该使用<code>SearchCondition或PageCondition</code>
 * 在combogrid里使用{@link AutoDataLoader}<font color='red'>必须指定{@link AutoDataResultClass}</font>
 * </pre>
 * @author jackson
 * @see ComboAutoLoaderHandler
 * @see AbstractAutoLoaderHandler
 * 
 * @see ComboBoxAutoLoaderHandler
 * @see ComboTreeAutoLoaderHandler
 * @see ComboTreeAjaxAutoLoaderHandler
 *
 */
public class ComboGridAutoLoaderHandler extends ComboAutoLoaderHandler{

	private EntityReader reader;
	// 使用查询条件的情况
	private ObjectDescriptor objectDescriptor;
	private Class<?> resultClass;
	
	public ComboGridAutoLoaderHandler(StandardAutoManager manager, Class<?> queryClass, AutoDataLoader loader, Field field) throws Exception {
		super(manager, queryClass, loader, field);
		__init(field);
	}

//	public ComboGridAutoLoaderHandler(StandardAutoManager manager, AutoDataLoader loader, Field field) throws Exception {
//		super(manager, loader, field);
//		__init(field);
//	}
	
	private void __init(Field field) throws Exception{
	//在combogrid里使用AutoDataLoader必须指定AutoDataResultClass
		AutoDataResultClass adrClass = field.getAnnotation(AutoDataResultClass.class);
		resultClass = adrClass.value();
		objectDescriptor = DescriptorFactory.objectDescriptor(resultClass);
		ApplicationContext applicationContext = manager.getApplicationContext();
		reader = BytecodeFactory.reader(applicationContext, objectDescriptor, entityDescriptor);
	
	}
	
	@Override
	protected StandardFinder buildFinder(ReadEvent event) throws Exception {
		return searcher.finder(entityManager, event, resultClass);//autoManager.buildeExtendFinder(reader, event);
	}

	@Override
	protected Page __parseResult(Finder finder, final ReadEvent event) throws Exception {
		if(isMultiple()){
			finder.setHandler(new FinderHandler() {
				final ItemSelectedFilter isf = new SelectedFilter(event);
				@Override
				public Object doHandler(Object source, Object obj) throws Exception {
					Item it = (Item)reader.read(event, obj);
					if(isf.doFilter(it)){
						it.setSelected(true);
					}
					return obj;
				}
			});
		}
		return finder.getPage();
	}

	public Collection<ObjectPropertyAccessor> proterties() {
		return objectDescriptor.values();
	}
}
